<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class rank_model extends CI_Model
{
    public function get_overall_rank($userID) {
        $tempList = array('college', 'school', 'major', 'minor');        
        $this->db->select('user_id, college, school, major, minor, user_score');
        $query = $this->db->get_where('user', array('user_id' => $userID));

        if ($query->num_rows() != 1) return FALSE;

        $user = $query->row_array();

        foreach ($tempList as $table) {
            $this->db->select($table . '_id, ' . $table . '_name');
            $query = $this->db->get_where($table, array($table . '_id' => $user[$table]));

            if ($query->num_rows() != 1 || $user[$table] == '') {
                $user[$table] = FALSE;
            }
            else {
                $user[$table] = $query->row_array();
                $this->db->select('user_id, user_score');
                $this->db->order_by('user_score', 'DESC');
                $rank = $this->db->get_where('user', array($table => $user[$table][$table . '_id']));
                $userList = $rank->result_array();

                foreach ($userList as $key => $value) {
                    if ($value['user_id'] == $user['user_id']) {
                        $user[$table]['rank'] = $key + 1;
                    }
                }                

                $this->db->select('user_score, AVG(user_score) AS average, COUNT(user_score) AS count');
                $rank = $this->db->get_where('user', array($table => $user[$table][$table . '_id']));
                $userList = $rank->row_array();
                $user[$table]['count'] = $userList['count'];
                $user[$table]['average'] = $userList['average'];
            }
        }
        return $user;
    }

    public function get_group_rank($userID, $sortCol = '') {
        $this->db->select('group_id');
        $query = $this->db->get_where('group_roster', array('user_id' => $userID, 'membership_status' => 1));
        if ($query->num_rows() < 1) return FALSE;
        $groupList = $query->result_array();

        foreach ($groupList as $group) {
            $this->db->select('group_id, group_name, group_type');
            $query = $this->db->get_where('group', array('group_id' => $group['group_id']));
            $data[] = $query->row_array();
        }

        $groupList = $this->sort_group_list($data);

        foreach ($groupList as $key => $group) {
            $this->db->select('user_id, user_score');
            $this->db->order_by('user_score', 'DESC');
            $query = $this->db->get_where('group_roster', array('group_id' => $group['group_id'], 'membership_status' => 1));
            $result = $query->result_array();

            foreach ($result as $rank => $student) {
                if ($student['user_id'] == $userID) {
                    $groupList[$key]['rank'] = $rank + 1;
                    $groupList[$key]['score'] = $student['user_score'];
                }
            }

            $query = $this->db->get_where('group_type', array('group_type_id' => $group['group_type']));

            if($query->num_rows() == 1) {
                $groupList[$key]['group_type'] = ucwords($query->row()->group_type_description);
            }


            $this->db->select('user_score, AVG(user_score) AS average, COUNT(user_score) AS count');
            $query = $this->db->get_where('group_roster', array('group_id' => $group['group_id'], 'membership_status' => 1));
            $row = $query->row_array();
            $groupList[$key]['count'] = $row['count'];
            $groupList[$key]['average'] = $row['average'];
        }

        if ($sortCol != '') $groupList = $this->sort_group_list($groupList, $sortCol);
        return $groupList;
    }

    public function sort_group_list($group_list, $sortCol = 'group_name')
    {
        $this->sortCol = $sortCol;
        usort($group_list, array($this, 'sort_list'));
        return $group_list;
    }

    public function sort_list($a, $b) { return $b[$this->sortCol] - $a[$this->sortCol]; }
}